import { ref } from 'vue'
import {
  connectServiceModality,
  getGoodListByManualId,
  getGoodListByVideoId,
  getGoods,
  getGoodWarrantyInfoByOrderId,
  getNotSpecGoodList,
  getSpareBoomListBySpareId,
} from '@/api/goodsInfo'
import { sparePageSelect, spareSpecPageSelect } from '@/api/code'

/**
 * 获取不包含规格的产品列表
 */
export const useGetNotSpecGoodList = () => {
  // 获取列表加载
  const getGoodListLoading = ref(false)

  // 获取产品列表请求参数
  const getGoodListParam = ref<GoodAPIRequest.GetNotSpecGoodList>({
    limit: 50,
    page: 1,
    brandId: '',
    typeId: '',
    typeThreeId: '',
    typeTwoId: '',
    goodsType: 1,
    keyword: '',
  })

  // 产品列表
  const goodList = ref<Array<NotSpecGoodListItem>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取产品列表
   */
  const getGoodList = async () => {
    getGoodListLoading.value = true
    const res = await getNotSpecGoodList(getGoodListParam.value)
    goodList.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getGoodListLoading.value = false
  }

  return {
    goodList,
    listTotal,
    getGoodListParam,
    getGoodListLoading,
    getGoodList,
  }
}

/**
 * 根据视频id获取产品列表
 */
export const useGetGoodListByVideoId = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 产品列表
  const list = ref<Array<GoodByVideoId>>([])

  /**
   * 获取列表
   * @param {String} id - 视频id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getGoodListByVideoId(id)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 根据说明书id获取产品列表
 */
export const useGetGoodListByManualId = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 产品列表
  const list = ref<Array<GoodByManualId>>([])

  /**
   * 获取列表
   * @param {String} id - 视频id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getGoodListByManualId(id)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 关联服务形式
 */
export const useConnectServiceModality = () => {
  // 关联加载
  const connectLoading = ref(false)

  // 关联表单
  const connectForm = ref<GoodAPIRequest.ConnectServiceModality>({
    productIds: [],
    productModuleRelationVos: [],
  })

  /**
   * 关联
   */
  const connect = async () => {
    connectLoading.value = true
    const res = await connectServiceModality(connectForm.value)
    connectLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    connectForm.value = {
      productIds: [],
      productModuleRelationVos: [],
    }
  }

  return {
    connectForm,
    connectLoading,
    connect,
    resetForm,
  }
}

/**
 * 获取物料BOM清单
 */
export const useGetSpareBomList = () => {
  // 获取清单加载
  const getListLoading = ref(false)

  // Bom清单
  const list = ref<Array<GoodSpareBom>>([])

  /**
   * 获取清单
   * @param id - 物料id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getSpareBoomListBySpareId(id)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 获取物料下拉列表
 */
export const useGetSparePartOptionList = () => {
  const getOptionListLoading = ref(false)
  const getOptionListParam = ref({
    name: '',
    page: 1,
    limit: 50,
  })
  const optionList = ref<Array<GoodSparePartOption>>([])

  const getSparePartOptionList = async (sparePartId?: string) => {
    getOptionListLoading.value = true
    const res = await sparePageSelect(getOptionListParam.value, sparePartId)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getSparePartOptionList,
  }
}

/**
 * 获取配件规格下拉选项
 */
export const useGetSparePartSpecOptionList = () => {
  const getOptionListLoading = ref(false)
  const getOptionListParam = ref({
    name: '',
    page: 1,
    limit: 50,
  })
  const optionList = ref<Array<GoodSparePartSpecOption>>([])

  const getOptionList = async (spartPartId: string) => {
    getOptionListLoading.value = true
    const res = await spareSpecPageSelect(getOptionListParam.value, spartPartId)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}

/**
 * 获取商品的质保信息
 */
export const useGetGoodWarrantyInfo = () => {
  // 获取质保信息加载
  const getGoodWarrantyInfoLoading = ref(false)

  // 商品质保信息列表
  const goodWarrantyInfoList = ref<Array<OrderGoodWarrantyInfo>>([])

  /**
   * 获取质保信息
   * @param {String} orderId - 工单id
   */
  const getGoodWarrantyInfo = async (orderId: string) => {
    getGoodWarrantyInfoLoading.value = true
    const res = await getGoodWarrantyInfoByOrderId(orderId)
    getGoodWarrantyInfoLoading.value = false
    if (res.data.code === 20000) {
      goodWarrantyInfoList.value = res.data.data.row
      return ''
    }
    return res.data.message
  }

  return {
    goodWarrantyInfoList,
    getGoodWarrantyInfoLoading,
    getGoodWarrantyInfo,
  }
}

/**
 * 获取商品列表
 */
export const useGetGoodList = () => {
  // 获取商品列表加载
  const getGoodListLoading = ref(false)

  // 获取商品列表请求参数
  const getGoodListParam = ref<GoodAPIRequest.GetGoodListParam>({
    brandId: '',
    goodsIdList: [],
    goodsType: '',
    keyword: '',
    limit: 50,
    page: 1,
    source: '',
    typeId: '',
    typeThreeId: '',
    typeTwoId: '',
    unitId: '',
    coreMaterials: null,
  })

  // 商品列表总数
  const goodListTotal = ref(0)

  // 商品列表
  const goodList = ref<Array<GoodItem>>([])

  /**
   * 获取商品列表
   */
  const getGoodList = async () => {
    getGoodListLoading.value = true
    const res = await getGoods(getGoodListParam.value)
    getGoodListLoading.value = false
    if (res.data.code === 20000) {
      goodListTotal.value = Number(res.data.data.total)
      goodList.value = res.data.data.rows
      return ''
    }
    return res.data.message
  }

  /**
   * 重置搜索条件
   */
  const resetGetGoodListParam = () => {
    getGoodListParam.value = {
      brandId: '',
      coreMaterials: null,
      goodsIdList: [],
      goodsType: '',
      keyword: '',
      limit: 50,
      page: 1,
      source: '',
      typeId: '',
      typeThreeId: '',
      typeTwoId: '',
      unitId: '',
    }
  }

  return {
    goodList,
    goodListTotal,
    getGoodListParam,
    getGoodListLoading,
    getGoodList,
    resetGetGoodListParam,
  }
}
